<%--
  Created by IntelliJ IDEA.
  User: FENG1
  Date: 2022/5/8
  Time: 16:06
  To change this template use File | Settings | File Templates.
--%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<%String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/";%>
<html>
<head>
    <base href="<%=basePath%>">
    <title>部门管理</title>
    <link rel="stylesheet" href="static/component/pear/css/pear.css" />
    <link rel="stylesheet" href="static/admin/css/other/department.css"/>
</head>
<body class="pear-container">
<div class="layui-card">
    <div class="layui-card-body">
        <table id="department-table" lay-filter="department-table" class="layui-hide"></table>
    </div>
</div>
</body>
<shiro:hasAnyRoles name="系统管理员">
    <script type="text/html" id="department-toolbar">
        <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
            <i class="layui-icon layui-icon-add-1"></i>
            新增
        </button>
    </script>
    <script type="text/html" id="department-bar">

        <button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit"><i
                class="layui-icon layui-icon-edit"></i></button>
        <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove"><i
                class="layui-icon layui-icon-delete"></i></button>
    </script>
</shiro:hasAnyRoles>
<script src="static/component/layui/layui.js"></script>
<script src="static/component/pear/pear.js"></script>
<script>
    layui.use(['table', 'form', 'jquery', 'dtree'], function() {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let dtree = layui.dtree;

        let MODULE_PATH = "operate/";

        let cols = [
            [
                {
                    title: '部门Id',
                    field: 'departmentId',
                    align: 'center'
                },
                {
                    title: '部门名称',
                    field: 'departmentName',
                    align: 'center'
                },
                {
                    title: '创建时间',
                    field: 'departmentTime',
                    align: 'center'
                },
                {
                    title: '操作',
                    toolbar: '#department-bar',
                    align: 'center',
                    width: 130
                }
            ]
        ]

        table.render({
            elem: '#department-table',
            url: 'department/allDep',
            page: false,
            cols: cols,
            skin: 'line',
            toolbar: '#department-toolbar',
            defaultToolbar: [{
                title: '刷新',
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }, 'filter', 'print', 'exports']
        });


        table.on('tool(department-table)', function(obj) {
            if (obj.event === 'remove') {
                window.remove(obj);
            } else if (obj.event === 'edit') {
                window.edit(obj);
            }
        });

        table.on('toolbar(department-table)', function(obj) {
            if (obj.event === 'add') {
                window.add();
            } else if (obj.event === 'refresh') {
                window.refresh();
            }
        });

        window.add = function() {
            var sign = "add";
            var did = 1;
            layer.open({
                type: 2,
                title: '新增',
                shade: 0.1,
                area: ['500px', '200px'],
                content:"department/allup?sign="+sign+"&did="+did
            });
        }
        //修改操作
        window.edit = function(obj) {
            let did = obj.data.departmentId;
            var sign = "up";
            layer.open({
                type: 2,
                title: '修改',
                shade: 0.1,
                area: ['500px', '200px'],
                content:"department/allup?sign="+sign+"&did="+did
            });
        }
        //删除操作
        window.remove = function(obj) {
            var did = obj.data.departmentId;
            alert(did);
            layer.confirm('确定要删除该用户', {
                icon: 3,
                title: '提示'
            }, function(index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url:"department/delDep?did="+did,
                    type: 'delete',
                    success: function(result) {
                        layer.close(loading);
                        if (result.success) {
                            layer.msg(result.msg, {
                                icon: 1,
                                time: 1000
                            }, function() {
                                obj.del();
                            });
                        } else {
                            layer.msg(result.msg, {
                                icon: 2,
                                time: 1000
                            });
                        }
                    }
                })
            });
        }
        window.refresh = function(param) {
            table.reload('department-table');
        }
    })
</script>
</html>